package entity;

import java.util.ArrayList;
import java.util.List;

public class SuitPile extends PokerPile{
	/**判断tail后面是否可以接head
	 * 
	 * @param tail 上一个牌组的最后一张牌
	 * @param head 下一个牌组的第一张牌
	 * @return 是否能接
	 */
	private boolean canContinue(Poker tail,Poker head){
		if(tail.getSuit()!=head.getSuit())	//花色不同的话	
			return false;	//不能接
		return tail.getNumber()+1==head.getNumber();//tail+1的数字必须等于head的数字
	}
	@Override
	public boolean put(List<Poker> pokers) {
		if(pokers.size()>1)	//如果想放入大于一张的牌
			return false;
		if(pokerList==null||pokerList.isEmpty()){//如果牌组为空
			if(pokers.get(0).getNumber()==1){	//如果放进来的是A
				pokerList.add(pokers.get(0));//接牌
				return true;
			}else{	//否则
				return false;
			}
		}
		if(!canContinue(pokerList.get(pokerList.size()-1), pokers.get(0)))	//如果不能接
			return false;	//返回false
		pokerList.add(pokers.get(0));//接牌
		return true;
	}

	@Override
	public List<Poker> get(int no) {
		if(no>=pokerList.size())	//如果序号大于当前牌数
			return null;	//返回空
		List<Poker> res=new ArrayList<>();
		res.add(pokerList.get(pokerList.size()-1));//将最后一张放入返回的集合
		pokerList.remove(pokerList.size()-1);//移走最后一张
		return res;
	}
	/**得到顶部的一张牌
	 * 
	 * @return
	 */
	public Poker getTop(){
		return pokerList.size()==0?null:pokerList.get(pokerList.size()-1);
	}
	public static void main(String[] args) {
		SuitPile test=new SuitPile();
		/*List<Poker> res=test.get(0);
		if(res==null)
			System.out.println("不能取牌");
		else{
			for(Poker poker:res){
				System.out.print(poker+" ");
			}
			System.out.println();
		}
		取牌测试
		*/
		/*放牌测试*/
		List<Poker> putList=new ArrayList<>();
		putList.add(new Poker(3, Suit.Heart));
		putList=new ArrayList<>();
		putList.add(new Poker(4, Suit.Heart));
		System.out.println(test.put(putList)?"成功放牌":"不能放牌");
		System.out.println(test);
		List<Poker> res=test.get(0);
		if(res==null)
			System.out.println("不能取牌");
		else{
			for(Poker poker:res){
				System.out.print(poker+" ");
			}
			System.out.println();
		}
		System.out.println(test);
	}
}
